package com.synnex.wms.controller;

import com.synnex.wms.pojo.Jiashu;
import com.synnex.wms.pojo.Tousu;
import com.synnex.wms.service.JiashuService;
import com.synnex.wms.service.TousuService;
import com.synnex.wms.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * TousuController   投诉、举报
 * */

@Controller
@RequestMapping(value = "/Tousu")
public class TousuController {

	//时间格式转换
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private TousuService TousuService;
	@Autowired
	private JiashuService JiashuService;

	 @RequestMapping(value = "/page")
	 public void page(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
        int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样
        PageUtil<Tousu> pageUtil = new PageUtil<Tousu>();//初始化工具类
        List<Tousu> list = new ArrayList<Tousu>();
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
        pageUtil.setPageIndex(pageIndex);//保存至工具类
        
        String username = request.getParameter("username");
        Tousu Tousu = new Tousu();
        Tousu.setUsername(username);
        
        int number = TousuService.pagecount(Tousu);//调用service层方法计算出总数据量，就是多少条数据.
        pageUtil.setPageNumber(number);//保存至工具类
        pageUtil.setPageSize(pageSize);//保存至工具类
        pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
        int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
        Tousu.setFenye(index);
        list = TousuService.showlist(Tousu);//调用service层的方法，取得数据库中的值
        pageUtil.setList(list);//保存到工具类中的集合
        request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		request.getRequestDispatcher("/jsp/Tousu/list.jsp").forward(request,
				response);
	}
	 
	 @RequestMapping(value = "/page2/{id}")
	 public void page2(@PathVariable Integer id,
			 HttpServletRequest request, HttpServletResponse response)
			 throws ServletException, IOException {
		 int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
		 int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样
		 PageUtil<Tousu> pageUtil = new PageUtil<Tousu>();//初始化工具类
		 List<Tousu> list = new ArrayList<Tousu>();
		 if (request.getParameter("pageIndex") != null) {
			 pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		 }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
		 pageUtil.setPageIndex(pageIndex);//保存至工具类
		 
		 String username = request.getParameter("username");
	     Tousu Tousu = new Tousu();
	     Tousu.setUsername(username);
		 Jiashu user = JiashuService.selectByPrimaryKey(id);
		 Tousu.setUid(id);
		 
		 int number = TousuService.pagecount(Tousu);//调用service层方法计算出总数据量，就是多少条数据.
		 pageUtil.setPageNumber(number);//保存至工具类
		 pageUtil.setPageSize(pageSize);//保存至工具类
		 pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
				 .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
		 int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
		 Tousu.setFenye(index);
		 list = TousuService.showlist(Tousu);//调用service层的方法，取得数据库中的值
		 pageUtil.setList(list);//保存到工具类中的集合
		 request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		 request.setAttribute("User", user);//传递到页面，存入值栈中
		 request.getRequestDispatcher("/jsp/Tousu/list2.jsp").forward(request,
				 response);
	 }
	 
	
	//删除
	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable Integer id) {
		Tousu ts = TousuService.selectByPrimaryKey(id);
		TousuService.deleteByPrimaryKey(id);
		return "redirect:/Tousu/page2/"+ts.getUid();
	
	}
	//添加
	@RequestMapping(value = "/insert")
	public String insert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uid = request.getParameter("uid");
		String introduce = request.getParameter("introduce");
		String username = request.getParameter("username");
		
		Tousu Tousu = new Tousu();
		Tousu.setIntroduce(introduce);
		Tousu.setUsername(username);
		
		Date currentTime = new Date();
		String csrq = formatter.format(currentTime);
		Tousu.setJbsj(csrq);
		Tousu.setZt(1);
		Tousu.setUid(Integer.parseInt(uid));
		
		TousuService.insert(Tousu);
		return "redirect:/Tousu/page2/"+Tousu.getUid();
	}
	
	//修改前查询
	@RequestMapping("/toUpdateLocPage/{id}")
	public void toUpdateLocPage(@PathVariable("id") Integer id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		Tousu Tousu = TousuService.selectByPrimaryKey(id);
		request.setAttribute("Tousu", Tousu);
		request.getRequestDispatcher("/jsp/Tousu/update.jsp").forward(request,
				response);

	}
	//添加前查询
	@RequestMapping("/a/{id}")
	public void a(@PathVariable("id") Integer id,
			HttpServletResponse response, HttpServletRequest request)
					throws ServletException, IOException {
		
		Jiashu Jiashu = JiashuService.selectByPrimaryKey(id);
		request.setAttribute("User", Jiashu);
		request.getRequestDispatcher("/jsp/Tousu/add.jsp").forward(request,
				response);
		
	}
	//修改
	@RequestMapping(value = "/update")
	public String update(Tousu Tousu) {
		TousuService.updateByPrimaryKeySelective(Tousu);
		return "redirect:/Tousu/page2/"+Tousu.getUid();
	}
	
	//审核通过
	@RequestMapping(value = "/shtg/{id}")
	public String shtg(@PathVariable Integer id) {
		Tousu ts = TousuService.selectByPrimaryKey(id);
		ts.setZt(2);
		TousuService.updateByPrimaryKeySelective(ts);
		return "redirect:/Tousu/page";
		
	}
	
	//审核不通过
	@RequestMapping(value = "/shbtg/{id}")
	public String shbtg(@PathVariable Integer id) {
		Tousu ts = TousuService.selectByPrimaryKey(id);
		ts.setZt(3);
		TousuService.updateByPrimaryKeySelective(ts);
		return "redirect:/Tousu/page";
		
	}
	

}
